package net.lanelife.casino.caipiao.dto.admin;

import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Selection;

import net.lanelife.casino.caipiao.entity.BalanceExchangeRecord;
import net.lanelife.casino.caipiao.entity.BalanceExchangeRecord_;
import net.lanelife.casino.common.dto.support.GenericTable;
import net.lanelife.casino.common.entity.json.serialize.CurrencySerializer;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.google.common.collect.Lists;

/**
 * @author Lane
 * 
 * 额度转换记录表格
 *
 * @param <DTO>
 */
public class BalanceExchangeRecordTable<DTO> extends GenericTable<DTO, BalanceExchangeRecord> {

	private static final long serialVersionUID = 2220607654117173017L;
	
	@JsonSerialize(using = CurrencySerializer.class)
	private Double amountSuccess = 0D;
	@JsonSerialize(using = CurrencySerializer.class)
	private Double amountFailure = 0D;
	@JsonSerialize(using = CurrencySerializer.class)
	private Double amountCancelled = 0D;
	
	public BalanceExchangeRecordTable() {}

	public BalanceExchangeRecordTable(Double amountSuccess,
			Double amountFailure, Double amountCancelled) {
		super();
		this.amountSuccess = amountSuccess;
		this.amountFailure = amountFailure;
		this.amountCancelled = amountCancelled;
	}

	@Override
	public List<Selection<?>> buildExpression(Root<BalanceExchangeRecord> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
		List<Selection<?>> columns = Lists.newArrayList();
		columns.add(builder.sum(builder.<Double>selectCase().when(builder.equal(root.get(BalanceExchangeRecord_.status), BalanceExchangeRecord.Status.SUCCESS), root.<Double>get(BalanceExchangeRecord_.amount)).otherwise(0D)));
		columns.add(builder.sum(builder.<Double>selectCase().when(builder.equal(root.get(BalanceExchangeRecord_.status), BalanceExchangeRecord.Status.FAILURE), root.<Double>get(BalanceExchangeRecord_.amount)).otherwise(0D)));
		columns.add(builder.sum(builder.<Double>selectCase().when(builder.equal(root.get(BalanceExchangeRecord_.status), BalanceExchangeRecord.Status.CANCELLED), root.<Double>get(BalanceExchangeRecord_.amount)).otherwise(0D)));
		return columns;
	}

	public Double getAmountSuccess() {
		return amountSuccess;
	}

	public void setAmountSuccess(Double amountSuccess) {
		this.amountSuccess = amountSuccess;
	}

	public Double getAmountFailure() {
		return amountFailure;
	}

	public void setAmountFailure(Double amountFailure) {
		this.amountFailure = amountFailure;
	}

	public Double getAmountCancelled() {
		return amountCancelled;
	}

	public void setAmountCancelled(Double amountCancelled) {
		this.amountCancelled = amountCancelled;
	}

}
